package com.bridgeintelligent.tag.bulkload.service.es;

import com.alibaba.fastjson.JSON;
import com.bridgeintelligent.tag.bulkload.api.fileload.Filter;
import com.bridgeintelligent.tag.bulkload.api.fileload.FilterGroup;
import com.bridgeintelligent.tag.constants.PublicConstant;
import com.bridgeintelligent.tag.mapper.DimDataMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author：liwei
 * @Date：2023/8/1 19:43
 * @Desc：
 */
@Component
@AllArgsConstructor
public class HiveSqlHelper {
    private DimDataMapper dimDataMapper;

    public String hander(FilterGroup filterGroup){

        StringBuffer buffer = new StringBuffer();
        if (filterGroup != null) {
            if (!CollectionUtils.isEmpty(filterGroup.getFilters())) {
                for (int i = 0; i < filterGroup.getFilters().size(); i++) {
                    if (i == (filterGroup.getFilters().size() - 1)) {
                        buffer.append(" ( ");
                        buffer.append(getValue(filterGroup.getFilters().get(i)));
                        buffer.append(" ) ");
                    } else {
                        buffer.append(" ( ");
                        buffer.append(getValue(filterGroup.getFilters().get(i)));
                        buffer.append(" ) ");
                        buffer.append(filterGroup.getSymbol());
                    }
                }
            }
            if (!CollectionUtils.isEmpty(filterGroup.getChildren())) {
                if (!CollectionUtils.isEmpty(filterGroup.getFilters())){
                    buffer.append(filterGroup.getSymbol());
                }
                for (int i = 0; i < filterGroup.getChildren().size(); i++) {
                    if (i == filterGroup.getChildren().size()-1){
                        buffer.append(" ( ");
                        buffer.append(hander(filterGroup.getChildren().get(i)));
                        buffer.append(" ) ");
                    }else{
                        buffer.append(" ( ");
                        buffer.append(hander(filterGroup.getChildren().get(i)));
                        buffer.append(" ) ");
                        buffer.append(filterGroup.getSymbol());
                    }

                }
            }
        }
        return buffer.toString();
    }
    public String getValue(Filter filter){

        StringBuffer buffer = new StringBuffer();
        List<String> valueList  = JSON.parseArray(filter.getValueListStr(), String.class);
        String classify = filter.getClassify();//标签类型
        String type = filter.getType();
        String largeType = dimDataMapper.getLargeType(filter.getDimId());
        if ("03".equals(classify)){ //如果是数值类
            if (!"termNum".equals(type) && !"noTermNum".equals(type)){
                if ("02".equals(largeType)){
                    //如果是定制标签需要将标签ID转成字符串
                    buffer.append( " cast ( ").append(filter.getDimId()).append(" as string )");
                }else{
                    buffer.append( " cast ( ").append(filter.getDimId()).append(" as double )");
                }
            }else{
                buffer.append(filter.getDimId());
            }
        }else{
            if ("02".equals(largeType)){
                if (!"term".equals(type) && !"noTerm".equals(type)){
                    //如果是定制标签需要将标签ID转成字符串
                    buffer.append( " cast ( ").append(filter.getDimId()).append(" as string )");
                }else{
                    buffer.append(filter.getDimId());
                }
            }else{
                buffer.append(filter.getDimId());
            }
        }
        buffer.append(getCurrentMonth(filter.getDataDate()));

        if("01".equals(classify)){ //描述类型 keyword
            if ("contain_vague".equals(type)){ //包含
                buffer.append(" like '%").append(valueList.get(0)).append("%' ");
            }else if ("not_contain_vague".equals(type)){ //不包含
                buffer.append(" not like '%").append(valueList.get(0)).append("%' ");
            }else if ("term".equals(type)){ //存在
                buffer.append(" is not null ");
            }else if ("noTerm".equals(type)){ //不存在
                buffer.append(" is null ");
            }
        }else if ("02".equals(classify)){ //标志类 keyword
            if ("equal".equals(type)){ // 等于
                buffer.append(" = ").append("'").append(valueList.get(0)).append("' ");
            }else if ("no_equal".equals(type)){ // 不等于
                buffer.append(" != ").append("'").append(valueList.get(0)).append("' ");
            }else if ("contain".equals(type)){ //包含
                buffer.append(" in (").append(value(valueList)).append(") ");
            }else if ("not_contain".equals(type)){ //不包含
                buffer.append(" not in (").append(value(valueList)).append(") ");
            }else if ("term".equals(type)){ //存在
                buffer.append(" is not null ");
            }else if ("noTerm".equals(type)){ //不存在
                buffer.append(" is null ");
            }
        }else if ("03".equals(classify)){ //数值类 double
            if ("greater".equals(type)){ // 大于
                buffer.append(" > ").append(valueList.get(0)).append(" ");
            }else if ("less".equals(type)){ // 小于
                buffer.append(" < ").append(valueList.get(0)).append(" ");
            }else if ("greater_equal".equals(type)){ //大于等于
                buffer.append(" >= ").append(valueList.get(0)).append(" ");
            }else if ("less_equal".equals(type)){ // 小于等于
                buffer.append(" <= ").append(valueList.get(0)).append(" ");
            }else if ("equal".equals(type)){ // 等于
                buffer.append(" = ").append(valueList.get(0)).append(" ");
            }else if ("no_equal".equals(type)){ // 不等于
                buffer.append(" != ").append(valueList.get(0)).append(" ").append(" or ")
                        .append(filter.getDimId()).append(getCurrentMonth(filter.getDataDate())+" is null");
            }else if ("among".equals(type)){ //在。。。之中
                buffer.append(" between ").append(valueList.get(0)).append(" and ").append(valueList.get(1)).append(" ");
            }else if ("not_among".equals(type)){ //不在。。。之中
                buffer.append(" not between ").append(valueList.get(0)).append(" and ").append(valueList.get(1)).append(" or ")
                        .append(filter.getDimId()).append(getCurrentMonth(filter.getDataDate())+" is null");
            }else if ("termNum".equals(type)){ // 存在
                buffer.append(" is not null "); //不存在
            }else if ("noTermNum".equals(type)){
                buffer.append(" is null ");
            }
        }else if ("05".equals(classify)){ //日期类 date
            if ("greater".equals(type)){ // 大于
                buffer.append(" > '").append(valueList.get(0)).append("' ");
            }else if ("less".equals(type)){ //小于
                buffer.append(" < '").append(valueList.get(0)).append("' ");
            }else if ("greater_equal".equals(type)){ // 大于等于
                buffer.append(" >= '").append(valueList.get(0)).append("' ");
            }else if ("less_equal".equals(type)){ //小于等于
                buffer.append(" <= '").append(valueList.get(0)).append("' ");
            }else if ("equal".equals(type)){ //等于
                buffer.append(" = ").append("'").append(valueList.get(0)).append("' ");
            }else if ("no_equal".equals(type)){ //不等于
                buffer.append(" != ").append("'").append(valueList.get(0)).append("' ");
            }else if ("among".equals(type)){ // 在。。。之中
                buffer.append(" between '").append(valueList.get(0)).append("' and '").append(valueList.get(1)).append("' ");
            }else if ("not_among".equals(type)){ // 不在。。。之中
                buffer.append(" not between '").append(valueList.get(0)).append("' and '").append(valueList.get(1)).append("' ");
            }else if ("term".equals(type)){ // 存在
                buffer.append(" is not null "); //不存在
            }else if ("noTerm".equals(type)){
                buffer.append(" is null ");
            }
        }else if ("06".equals(classify)){ // 集合类 keyword
            if ("contain_everything".equals(type)){
                int i = 0;
                String name = buffer.toString();
                for (String value:valueList){
                    if(i==0){
                        i++;
                        buffer.append(" like '%").append(value).append("%' ");
                        continue;
                    }
                    buffer.append(" or ").append(name).append(" like '%").append(value).append("%' ");
                }
            }
        }
        return buffer.toString();
    }
    public static String value(List<String> valus){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < valus.size(); i++) {
            if (i==valus.size()-1){
                buffer.append("'").append(valus.get(i)).append("'");
            }else{
                buffer.append("'").append(valus.get(i)).append("',");
            }
        }
        return buffer.toString();
    }

    /**
     * 处理特殊时间格式
     * @param dataDate
     * @return
     */
    public static String getCurrentMonth(String dataDate){
        return PublicConstant.ZERO.equals(dataDate)?"":dataDate;
    }


}
